import time
from selenium import webdriver
from selenium.webdriver.common.by import By
import unittest
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from A3mall.a3mall_db import Mydb


class A3mall(unittest.TestCase):

    def setUp(self) -> None:
        # 连接数据库
        self.db = Mydb('10.59.9.14', 'root', 'yidong666', 'a3mall', '3306')
        self.driver = webdriver.Firefox()
        self.driver.maximize_window()
        self.driver.get('http://10.59.9.14:7003/')
        # 隐式等待
        self.driver.implicitly_wait(10)

    def tearDown(self) -> None:
        # 关闭数据库连接
        self.db.close()
        time.sleep(5)
        # 退出浏览器
        self.driver.quit()

    # 正例1：case_001：输入正确的用户名密码登录成功
    # 用户总金额
    userprice = None

    def test_a3_01(self):
        # 1、点击登录
        self.driver.find_element(By.XPATH, '//a[@href="/home/login.html"]').click()
        # 断言1：会员登录
        dy1 = self.driver.find_element(By.CLASS_NAME, 'reg-new').text
        print(f'dy1:{dy1}')
        self.assertEqual('会员登录', dy1)
        # 2、输入用户名
        self.driver.find_element(By.NAME, 'username').send_keys('15985645484')
        # 3、输入密码
        self.driver.find_element(By.NAME, 'password').send_keys('123456')
        # 4、勾选记住密码
        self.driver.find_element(By.CSS_SELECTOR, '#theForm > div.agree-clause > span > label > input').click()
        # 强制等待10秒，输入验证码
        time.sleep(10)
        # 5、点击登录按钮
        self.driver.find_element(By.CLASS_NAME, 'finish-btn').click()
        time.sleep(5)
        # 断言2：欢迎回来，小高4
        dy2 = self.driver.find_element(By.XPATH, '/html/body/div[5]/div[2]/div[1]/div[2]/div[2]/ul/li[1]').text
        print(f'dy2:{dy2}')
        self.assertIn('欢迎回来', dy2)
        price = self.driver.find_element(By.XPATH, '/html/body/div[5]/div[2]/div[1]/div[2]/div[2]/ul/li[4]/span').text
        price1 = str(price).split('¥ ')
        print(f"用户总金额:{price1[1]}")
        A3mall.userprice = float(price1[1])

    # 正例2：case_006：成功购买商品，并生成待发货订单
    def test_a3_02(self):
        # 前置条件 ：账号已登录
        self.test_a3_01()
        time.sleep(1)
        # 1、输入关键字搜索
        ss = self.driver.find_element(By.TAG_NAME, 'input')
        # 鼠标右击事件
        ActionChains(self.driver).context_click(ss).perform()
        time.sleep(2)
        # 鼠标释放事件
        ActionChains(self.driver).release(ss).perform()
        time.sleep(2)
        # 键盘输入事件
        ss.send_keys('水瓶座')
        time.sleep(2)
        # 键盘退格事件
        ss.send_keys(Keys.BACKSPACE)
        time.sleep(1)
        # 键盘回车事件
        ss.send_keys(Keys.ENTER)
        time.sleep(3)
        # 浏览器返回事件
        self.driver.back()
        time.sleep(2)
        # 浏览器前进事件
        self.driver.forward()
        time.sleep(2)
        # 浏览器刷新事件
        self.driver.refresh()
        time.sleep(2)
        # 2、双击要下单的商品
        sp1 = self.driver.find_element(By.XPATH, '/html/body/div[5]/div[2]/div[2]/div[1]/ul/li/div[1]/span/a/img')
        # 鼠标双击事件
        ActionChains(self.driver).double_click(sp1).perform()
        # 断言3：断言商品详情的商品名称：性能测试-水瓶
        # 隐式等待
        self.driver.implicitly_wait(5)
        dy3 = self.driver.find_element(By.XPATH, '//*[@id="goodsForm"]/div/div[1]/span').text
        print(f'dy3:{dy3}')
        self.assertEqual('性能测试-水瓶', dy3)
        # 3、选择购买的商品数量
        num = self.driver.find_element(By.XPATH,
                                       '/html/body/div[5]/div/div[2]/form/div/div[4]/div[1]/div[1]/div[1]/input')
        num.clear()
        time.sleep(1)
        num.send_keys('2')
        # 4、点击立即购买
        self.driver.find_element(By.ID, 'buy-cart').click()
        # 隐式等待
        self.driver.implicitly_wait(5)
        # 断言4：断言进入确认订单页面
        dy4 = self.driver.find_element(By.XPATH, '/html/body/div[4]/div[1]/a[2]').text
        print(f'dy4:{dy4}')
        self.assertEqual('确认订单', dy4)
        # 隐式等待
        self.driver.implicitly_wait(5)
        # 断言5：断言收货地址信息包含：15985645484
        dy5 = self.driver.find_element(By.XPATH, '//*[@id="theForm"]/div[1]/div[2]/div/label').text
        print(f'dy5:{dy5}')
        self.assertIn('15985645484', dy5)
        # 5、点击下一步
        self.driver.find_element(By.ID, 'order-button').click()
        # 隐式等待
        self.driver.implicitly_wait(5)
        # 断言6：断言进入订单提交页面
        dy6 = self.driver.find_element(By.XPATH, '/html/body/div[4]/div[1]/a[2]').text
        print(f'dy6:{dy6}')
        self.assertEqual('订单完成', dy6)
        # 隐式等待
        self.driver.implicitly_wait(5)
        # 断言7：断言提交订单成功
        dy7 = self.driver.find_element(By.XPATH, '/html/body/div[4]/div[3]/span').text
        print(f'dy7:{dy7}')
        self.assertIn('支付成功', dy7)
        # 6、点击查看订单
        self.driver.find_element(By.XPATH, '/html/body/div[4]/div[3]/p/a').click()
        # 隐式等待
        self.driver.implicitly_wait(5)
        # 断言8：断言进入订单详情页面
        dy8 = self.driver.find_element(By.XPATH, '/html/body/div[5]/div[2]/div/div[1]/span').text
        print(f'dy8:{dy8}')
        self.assertEqual('我的订单', dy8)
        # 隐式等待
        self.driver.implicitly_wait(5)
        # 断言9：断言生成了订单号
        dy9 = self.driver.find_element(By.XPATH, '/html/body/div[5]/div[2]/div/div[2]/table[2]/tbody/tr[2]/td[2]').text
        print(f'day9:{dy9}')
        self.assertTrue(dy9)
        # 隐式等待
        self.driver.implicitly_wait(5)
        # 断言10：数据库断言：用户总金额-订单支付金额=用户余额
        # 订单支付金额
        yf_price = self.driver.find_element(By.XPATH, '/html/body/div[5]/div[2]/div/div[5]/div/div/div[2]/span').text
        orderprice = float(str(yf_price).split('￥￥')[1])
        print(f"订单支付金额：{orderprice}")
        # 用户余额
        amount = self.db.seatch(f"select amount from mall_users where mobile = '{15985645484}'")[0][0]
        print(f"用户余额：{amount}")
        self.assertEqual(str(A3mall.userprice - orderprice), str(amount))

    # 反例：case_002：密码输入错误登录失败
    def test_a3_03(self):
        # 1、点击登录
        self.driver.find_element(By.XPATH, '//a[@href="/home/login.html"]').click()
        # 断言11：会员登录
        dy11 = self.driver.find_element(By.CLASS_NAME, 'reg-new').text
        print(f'dy11:{dy11}')
        self.assertEqual('会员登录', dy11)
        # 2、输入用户名
        self.driver.find_element(By.NAME, 'username').send_keys('15985645484')
        # 3、输入密码
        self.driver.find_element(By.NAME, 'password').send_keys('123456Aa')
        # 4、勾选记住密码
        self.driver.find_element(By.CSS_SELECTOR, '#theForm > div.agree-clause > span > label > input').click()
        # 强制等待15秒，输入验证码
        time.sleep(15)
        # 5、点击登录按钮
        self.driver.find_element(By.CLASS_NAME, 'finish-btn').click()
        # 断言12：断言登录失败，还在登录页面
        dy12 = self.driver.find_element(By.XPATH, '/html/body/div[4]/div/div[1]/div[1]/span[1]').text
        print(f'dy12:{dy12}')
        self.assertEqual('会员登录', dy12)


if __name__ == '__main__':
    unittest.main()
